//
//  GXQueryHistoryViewController.swift
//  GXKit_Swift
//
//  Created by guangxiao on 2018/1/16.
//  Copyright © 2018年 guangxiao. All rights reserved.
//

import UIKit

class GXQueryHistoryViewController: UIViewController {

//    var tableView:UITableView!
    var foreignDataSource:[GXForeignCellModel] = [GXForeignCellModel]()
    var inlandDataSource:[GXInlandCellModel] = [GXInlandCellModel]()
    var currentRecruitmentArea:RecruitmentArea = .foreign
    var foreignVC:GXQueryListInnerViewController = GXQueryListInnerViewController(recruitArea: .foreign)
    var inlandVC:GXQueryListInnerViewController = GXQueryListInnerViewController(recruitArea: .inland)
    var scrollView:UIScrollView!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "编辑", style: .done, target: self, action: #selector(GXQueryHistoryViewController.edit(sender:)))
        setupUI()
    }
   
    private func setupUI(){
        topBar.selectedItemWithTagClosure = {[weak self] tag in
            guard let `self` = self else{return}
            let x = CGFloat(tag - 1200) * GXConfig.Constant.ScreenWidth
            self.scrollView.scrollRectToVisible(CGRect(x:x,y:0,width:GXConfig.Constant.ScreenWidth,height:GXConfig.Constant.ScreenHeight - self.topBar.frame.height - GXConfig.Constant.NavBarHeight), animated: true)

        }
        view.addSubview(topBar)
        topBar.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(GXConfig.Constant.NavBarHeight)
            make.left.right.equalTo(view)
            make.height.equalTo(46)
        }
        
        initScrollView()
        
        inlandVC.inlandDataSource = [GXInlandCellModel(),GXInlandCellModel(),GXInlandCellModel(),GXInlandCellModel(),GXInlandCellModel(),GXInlandCellModel(),GXInlandCellModel(),GXInlandCellModel()]
        
        foreignVC.foreignDataSource = [GXForeignCellModel(),GXForeignCellModel(),GXForeignCellModel(),GXForeignCellModel(),GXForeignCellModel(),GXForeignCellModel(),GXForeignCellModel()]
    }
    
    private func initScrollView(){
        scrollView = UIScrollView(frame: CGRect.zero)
        scrollView.delegate = self
        
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(topBar.snp.bottom)
            make.left.right.bottom.equalTo(view)
        }
        
        scrollView.contentSize = CGSize(width: GXConfig.Constant.ScreenWidth * 2, height: GXConfig.Constant.ScreenHeight - topBar.frame.height - GXConfig.Constant.NavBarHeight)
        let contentView = UIView(frame: CGRect(x: 0, y: 0, width: GXConfig.Constant.ScreenWidth * 2, height: GXConfig.Constant.ScreenHeight - topBar.frame.height - GXConfig.Constant.NavBarHeight))
        
        scrollView.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.edges.height.equalTo(scrollView)
            make.width.equalTo(GXConfig.Constant.ScreenWidth * 2)
        }
        scrollView.isPagingEnabled = true
        addChildViewController(foreignVC)
        addChildViewController(inlandVC)
        
        contentView.addSubview(foreignVC.view)
        contentView.addSubview(inlandVC.view)
        
        foreignVC.view.snp.makeConstraints { (make) in
            make.top.left.bottom.equalTo(contentView)
            make.width.equalTo(GXConfig.Constant.ScreenWidth)
        }
        inlandVC.view.snp.makeConstraints { (make) in
            make.top.right.bottom.equalTo(contentView)
            make.width.equalTo(GXConfig.Constant.ScreenWidth)
        }

    }
    @objc private func edit(sender:UIBarButtonItem){
//        if self.tableView.isEditing{
//            let selectedCount = tableView.selectedTestCells().count
//            print(selectedCount)
//
//            sender.title = "编辑"
//            self.tableView.setEditing(false, animated: true)
//        }else{
//            sender.title = "完成"
//            self.tableView.setEditing(true, animated: true)
//        }
    }

    
    lazy var topBar:GXTopSlideBar = {
        let topBar = GXTopSlideBar(itemTitles: ["国外","国内"], frame: CGRect(x: 0, y: 0, width: GXConfig.Constant.ScreenWidth, height: 46))
        
        return topBar
    }()
}
extension GXQueryHistoryViewController:UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        topBar.scrollSmothly(scrollView.contentOffset.x)
    }

}





/// 查询区域
///
/// - foreign: 国外
/// - inland: 国内
enum RecruitmentArea:Int {
    case foreign = 0
    case inland = 1
}

/// 查询类型
///
/// - immediate: 即时查询
/// - allTheWay: 全程查询
enum RecruitmentType{
    case immediate
    case allTheWay
}
